package cn.sc.hunter.framework.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * 编码和解码工具类
 */
public final class CodecUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(CodecUtil.class);

    /**
     * URL 编码
     */
    public static String encodeURL(String source) {
        String target;
        try {
            target = URLEncoder.encode(source, Constants.URL_ENCODING);
        } catch (Exception e) {
            LOGGER.error("URL 编码失败", e);
            throw new RuntimeException(e);
        }
        return target;
    }

    /**
     * URL 解码
     */
    public static String decodeURL(String source) {
        String target;
        try {
            target = URLDecoder.decode(source, Constants.URL_ENCODING);
        } catch (Exception e) {
            LOGGER.error("URL 解码失败", e);
            throw new RuntimeException(e);
        }
        return target;
    }
}
